﻿namespace WeDonekRpc.SqlSugar.Tran
{
    internal class LocalTransaction : ILocalTransaction
    {
        private readonly ISqlClientFactory _Factory;
        private readonly ILocalTran _Client;
        private readonly bool _IsInherit;
        public LocalTransaction ( ISqlClientFactory factory, bool isInherit )
        {
            this._Factory = factory;
            if ( factory.IsTran && isInherit )
            {
                this._IsInherit = true;
                return;
            }
            else if ( factory.IsTran )
            {
                this._Client = factory.Current;
            }
            this._Factory.BeginTran();
        }


        public void Commit ()
        {
            if ( this._IsInherit )
            {
                return;
            }
            this._Factory.CommitTran(this._Client);
        }
        public void Dispose ()
        {
            if ( this._IsInherit )
            {
                return;
            }
            this._Factory.RollbackTran(this._Client);
        }
    }
}
